import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_book_store/home_model.dart';
import 'package:flutter_book_store/user/user.dart';
import 'package:get/get.dart';

import 'book/book_list.dart';
import 'routes/app_pages.dart';

class HomePageWidget extends StatefulWidget {
  @override
  _HomePageWidgetState createState() => _HomePageWidgetState();
}

class _HomePageWidgetState extends State<HomePageWidget> {
  var _controller;
  var titles = ['主页', '我的借阅', '个人中心'];
  int _currentIndex = 0;
  var title;
  List<BottomNavigationBarItem> icons = List();
   HomeModel _homeModel;
  @override
  void initState() {
    _homeModel = Get.find();
    // TODO: implement initState
    _controller = PageController(
      initialPage: _currentIndex,
    );
    title = titles[_currentIndex];
    super.initState();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('明日图书馆'),
        actions: [
          Obx(() => _homeModel.name.value == ''
              ? LoginButton()
              : LoginedButton(
            viewModel: _homeModel,
          ))
        ],
        leading: Padding(
          padding: const EdgeInsets.only(left: 8),
          child:
              Image.network('http://123.57.135.227:7857/img/logo.f278d355.jpg'),
        ),
        centerTitle: true,
      ),
      body: PageView(
        controller: _controller,
        children: [BookListPage(), UserPage(), UserPage()],
        physics: NeverScrollableScrollPhysics(),
      ),
      bottomNavigationBar: BottomNavigationBar(
          currentIndex: _currentIndex,
          backgroundColor: Colors.white,
          selectedItemColor: Colors.blue,
          unselectedItemColor: Colors.grey.shade600,
          selectedFontSize: 12,
          unselectedFontSize: 12,
          onTap: (index) async {
            title = titles[index];
            _controller.jumpToPage(index);
            setState(() {
              _currentIndex = index;
            });
          },
          type: BottomNavigationBarType.fixed,
          items: setIcons()),
    );
  }

  List<BottomNavigationBarItem> setIcons() {
    icons.clear();
    icons.add(BottomNavigationBarItem(
        icon: _currentIndex == 0
            ? Icon(Icons.home, color: Colors.blue)
            : Icon(Icons.home, color: Colors.grey.shade600),
        title: Text(
          titles[0],
        )));
    icons.add(BottomNavigationBarItem(
        icon: _currentIndex == 1
            ? Icon(Icons.drive_eta, color: Colors.blue)
            : Icon(Icons.drive_eta, color: Colors.grey.shade600),
        title: Text(
          titles[1],
        )));
    icons.add(BottomNavigationBarItem(
        icon: _currentIndex == 2
            ? Icon(Icons.person, color: Colors.blue)
            : Icon(Icons.person, color: Colors.grey.shade600),
        title: Text(
          titles[2],
        )));

    return icons;
  }
}


class LoginButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return   Padding(
      padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 8),
      child: OutlinedButton(
        style: TextButton.styleFrom(
            minimumSize: Size(15, double.infinity),
            backgroundColor: Colors.transparent,
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.all(Radius.circular(5))),
            side: BorderSide(color: Colors.blue, width: 1)),
        onPressed: () {
          Get.toNamed(Routes.LOGIN);
        },
        child: Row(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Icon(
              Icons.person,
              size: 14,
              color: Colors.blue,
            ),
            Padding(
              padding: const EdgeInsets.only(left: 4),
              child: Text(
                '登录',
                style: TextStyle(fontSize: 12, color: Colors.black),
              ),
            )
          ],
        ),
      ),
    );
  }
}

class LoginedButton extends StatelessWidget {
  final viewModel;

  const LoginedButton({Key key, this.viewModel}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: (){
        viewModel.name.value='';
      },
      child: Padding(
        padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 8),
        child: SizedBox(
          width: 60,
          child: Row(
            children: [
              Icon(
                Icons.person,
                size: 14,
                color: Colors.blue,
              ),
              Expanded(
                child: Text(
                  viewModel.name.value,
                  overflow: TextOverflow.ellipsis,
                  maxLines: 1,
                  style: TextStyle(fontSize: 14, color: Colors.black),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}

